/* eslint-disable no-use-before-define */
import axios, { type Paging, type Response } from '@/utils/axios/axios';
import type { AppUserSavings } from '@/api/app-user-management.ts';

/** 获取今日数据监控列表 */
export function getDistributionData(data: DistributionRequest): Promise<Response<DistributionData>> {
  const _data = JSON.parse(JSON.stringify(data));
  // eslint-disable-next-line no-bitwise
  if (!~_data.range) {
    delete _data.range;
  }
  return axios.post(`/system/distribution/list`, data) as unknown as Promise<Response<DistributionData>>;
}

/** 通过用户id拿短剧id列表 */
export function getDistributionDataById(): Promise<Response<{ dramaId: number; dramaTitle: string }[]>> {
  return axios.get(`/system/distribution/byId`) as unknown as Promise<Response<{ dramaId: number; dramaTitle: string }[]>>;
}

/** 短剧与用户关联的分页列表 */
export function getDistributionDataControlList(params: DistributionDataControlRequest): Promise<Response<Paging<DistributionDataControlItem>>> {
  return axios.get(`/system/distribution/page`, { params }) as unknown as Promise<Response<Paging<DistributionDataControlItem>>>;
}

/** 查询公司权限范围内的短剧page */
export function getDistributionDataDramaList(): Promise<Response<DistributionDataControlDramaItem[]>> {
  return axios.get(`/system/distribution/listDrama`) as unknown as Promise<Response<DistributionDataControlDramaItem[]>>;
}

/** 给投手设置短剧权限 */
export function setDistributionData(data: setDistributionDataRequest): Promise<Response<string>> {
  return axios.post(`/system/distribution/set`, data) as unknown as Promise<Response<string>>;
}

/** 根据某些状态的订单列表 */
export function getOrderByCondition(data: OrderByConditionRequest): Promise<Response<Paging<AppUserSavings>>> {
  const _data = JSON.parse(JSON.stringify(data));
  // eslint-disable-next-line no-bitwise
  if (!~_data.range) {
    delete _data.range;
  }
  _data.dramaIds = Array.isArray(_data.dramaIds) ? _data.dramaIds.join(',') : '';
  return axios.get(`/system/order/byCondition`, { params: _data }) as unknown as Promise<Response<Paging<AppUserSavings>>>;
}

export interface DistributionRequest {
  /**
   * 短剧ids
   */
  dramaIds?: number[];
  /**
   * 结束时间
   */
  endDate?: string;
  /**
   * 数据范围 1今日 2昨日 3本周 4本月 不传查全部
   */
  range?: number;
  /**
   * 开始时间
   */
  startDate?: string;
  /**
   * 投广渠道
   */
  investmentChannel: string;
}

export interface OrderByConditionRequest extends DistributionRequest {
  /**
   * 1.已付款订单 2.未付款订单 3.首冲订单 4.复充订单
   */
  condition?: number;
  current: number;
  size: number;
}

export interface DistributionData {
  /**
   * 首充总单数
   */
  firstNums?: number;
  /**
   * 总首充金额
   */
  firstRecharge?: number;
  /**
   * 首次复充金额
   */
  firstRepeatAmount?: number;
  /**
   * 已付款总订单数
   */
  paidNums?: number;
  /**
   * 充值用户ARPU（总充值金额/充值用户数）
   */
  rechargeArpu?: number;
  /**
   * 复充总人数
   */
  repeatMens?: number;
  /**
   * 复充总单数
   */
  repeatNums?: number;
  /**
   * 总复充金额
   */
  repeatRecharge?: number;
  /**
   * 总充值金额
   */
  totalRecharge?: number;
  /**
   * 总注册用户数
   */
  totalUsers?: number;
  /**
   * 未付款订单数
   */
  unpaidNums?: number;
  /**
   * 注册用户ARPU（总充值金额/总注册用户数）
   */
  userArpu?: number;
  /**
   * 消耗
   */
  cost?: number | null;
  /**
   * 分成金额
   */
  dividendAmount?: number;
  /**
   * 利润=充值-消耗
   */
  profit?: number;
}

export interface DistributionDataControlRequest {
  current: number;
  size: number;
  dramaId?: string;
  dramaTitle?: string;
  createBy?: string;
}

export interface DistributionDataControlDramaItem {
  /**
   * 短剧id
   */
  dramaId: number;
  /**
   * 短剧名称
   */
  dramaTitle?: string;
}

export interface DistributionDataControlItem extends DistributionDataControlDramaItem {
  /**
   * 用户名
   */
  createBy?: string;
  /**
   * 用户ID
   */
  userId?: number;
}

/**
 * 更新参数
 *
 * DramaMangerUpdateModel
 */
export interface setDistributionDataRequest {
  /**
   * 短剧id集合
   */
  dramaIds?: number[];
  /**
   * 短剧名集合
   */
  dramaTitles?: string[];
  /**
   * 用户id
   */
  userId?: number;
  /**
   * 是否能看金额
   */
  seeMoney?: number;
}
